<?php namespace Home\Controller;

use Framework\Kernel\Controller;
//问题类
use Home\Model\Ask;
//数据库
use Framework\Model\Db;
//分类库
use Home\Model\Category;

class AskController extends Controller{
	//
	protected $add;

	public function __init()
	{
		$this->add = new Ask;
	}

	public function ask()
	{
		// $this->display('ask.html');
		//判断如果$_SESSION['id']为空，也就是没有登录的时候，不能跳到提问页进行提问
		if(empty($_SESSION['id']))
		{
			$this->error('请先登录',U('Index.index'));
		}
		else{
			//如果提问的表单框不为空，那么运行Ask.php里的ask方法
			if(!empty($_POST['content']))
			{

				// 如果数据成功压入数据库，调用成功的方法，再跳转回首页
				if($this->add->ask_exec())
				{
					// $this->success('发表成功',U('index',['id'=>$id]));
					$this->success('发表成功',U('Home.Index.index'));		
				}else{
					//不成功表示没有数据，调用错误方法
					echo 999;
					echo $this->error('error');
				}				
			}else
			{
				$this->assign('category',(new Category)->getAll());
				$this->display('ask.html');
			}
		}
	}

	//问题展示页面
	public function show()
	{
		//获得传入的id，得到相应的问题内容
		$id = $_GET['id'];		
		$field = $this->add->where("id=$id")->first();
		// $field = (new Db('question'))->query("select * from question as q join category as c on q.cat_id=c.id 
		// 	where q.id=$id");
		// p($field);
		$this->assign('field',$field);

		$answer = (new Db('answer'))->query('select * from answer where aid=? limit 4',[$id]);
		$this->assign('answer',$answer);
		// p($answer);
		$this->display('show.html');
	}


}